// exports.ListViewDir = exports.ListView = void 0;
// var i;
// var r;
// var AL_GAME_HUA_layout_utils = require("./AL_GAME_HUA_layout_utils");
// var AL_GAME_HUA_listviewitem = require("./AL_GAME_HUA_listviewitem");
// var c = (function () {
//     function e(e) {
//         this._xuanzhong_de_suoyin = -1;
//         this.scrollview = e.scrollview;
//         this.mask = e.mask;
//         this.content = e.content;
//         this.item_tpl = e.item_tpl;
//         this.item_tpl instanceof cc.Node
//             ? ((this.item_width = this.item_tpl.width),
//               (this.item_height = this.item_tpl.height),
//               (this.item_tpl.active = !1))
//             : ((this.item_width = this.item_tpl.data.width), (this.item_height = this.item_tpl.data.height));
//         this.dir = e.direction || i.Vertical;
//         this.width = e.width || this.scrollview.node.width;
//         this.height = e.height || this.scrollview.node.height;
//         this.gap_x = e.gap_x || 0;
//         this.gap_y = e.gap_y || 0;
//         this.padding_left = e.padding_left || 0;
//         this.padding_right = e.padding_right || 0;
//         this.padding_top = e.padding_top || 0;
//         this.padding_bottom = e.padding_bottom || 0;
//         this.item_anchorX = null != e.item_anchorX ? e.item_anchorX : 0;
//         this.item_anchorY = null != e.item_anchorY ? e.item_anchorY : 1;
//         this.row = e.row || 1;
//         this.col = e.column || 1;
//         this.cb_host = e.cb_host;
//         this.scroll_to_end_cb = e.scroll_to_end_cb;
//         this.auto_scrolling = e.auto_scrolling || !1;
//         this.item_pool = [];
//         if (this.dir == i.Vertical) {
//             var t = (this.item_width + this.gap_x) * this.col - this.gap_x + this.padding_left + this.padding_right;
//             if (t > this.width) {
//                 cc.log("content_width > width, resize listview to content_width,", this.width, "->", t);
//                 this.width = t;
//             }
//             this.set_content_size(this.width, 0);
//         } else {
//             var n = (this.item_height + this.gap_y) * this.row - this.gap_y + this.padding_top + this.padding_bottom;
//             if (n > this.height) {
//                 cc.log("content_height > height, resize listview to content_height,", this.height, "->", n);
//                 this.height = n;
//             }
//             this.set_content_size(0, this.height);
//         }
//         this.original_width = this.width;
//         this.original_height = this.height;
//         this.mask.node.setContentSize(this.width, this.height);
//         this.scrollview.node.setContentSize(this.width, this.height);
//         this.scrollview.vertical = this.dir == i.Vertical;
//         this.scrollview.horizontal = this.dir == i.Horizontal;
//         this.scrollview.inertia = !0;
//         this.scrollview.node.on("scrolling", this.on_scrolling, this);
//         this.scrollview.node.on("scroll-to-bottom", this.on_scroll_to_end, this);
//         this.scrollview.node.on("scroll-to-right", this.on_scroll_to_end, this);
//         this.scrollview.node.on(cc.Node.EventType.TOUCH_START, this.on_scroll_touch_start, this);
//         this.scrollview.node.on(cc.Node.EventType.TOUCH_END, this.on_scroll_touch_end, this);
//         this.scrollview.node.on(cc.Node.EventType.TOUCH_CANCEL, this.on_scroll_touch_cancel, this);
//     }
//     e.prototype.on_scroll_touch_start = function (e) {
//         this._touchBeganPosition = e.touch.getLocation();
//     };
//     e.prototype.on_scroll_touch_cancel = function (e) {
//         this._touchEndPosition = e.touch.getLocation();
//         this.handle_release_logic();
//     };
//     e.prototype.on_scroll_touch_end = function (e) {
//         this._touchEndPosition = e.touch.getLocation();
//         this.handle_release_logic();
//     };
//     e.prototype.handle_release_logic = function () {
//         if (this._touchBeganPosition) {
//             var e = this._touchEndPosition;
//             var t = this._touchBeganPosition.sub(this._touchEndPosition);
//             if (0 == this.get_drag_direction(t) && this.packItems && this.packItems.length) {
//                 var n = this.content.convertToNodeSpaceAR(e);
//                 for (var o = this._qishi_suoyin; o <= this._tingzhi_suoyin; o++) {
//                     var i = this.packItems[o];
//                     if (i && i.item && i.item.node.getBoundingBox().contains(n)) {
//                         i.item.handle_event_touch_end(i.item.node.convertToNodeSpaceAR(e), i.data, o);
//                         break;
//                     }
//                 }
//             }
//         }
//     };
//     e.prototype.get_drag_direction = function (e) {
//         return this.dir === i.Horizontal
//             ? Math.abs(e.x) < 3
//                 ? 0
//                 : e.x > 0
//                 ? 1
//                 : -1
//             : this.dir === i.Vertical
//             ? Math.abs(e.y) < 3
//                 ? 0
//                 : e.y < 0
//                 ? 1
//                 : -1
//             : void 0;
//     };
//     e.prototype.on_scroll_to_end = function () {
//         if (this.scroll_to_end_cb) {
//             this.scroll_to_end_cb.call(this.cb_host);
//         }
//     };
//     e.prototype.on_scrolling = function () {
//         var e;
//         var t;
//         this.dir == i.Vertical
//             ? ((e = this.content.y), (t = this.item_height))
//             : ((e = this.content.x), (t = this.item_width));
//         if (!(null != this.last_content_pos && Math.abs(e - this.last_content_pos) < t)) {
//             this.last_content_pos = e;
//             this.render();
//         }
//     };
//     e.prototype.render = function () {
//         if (this.packItems && this.packItems.length) {
//             if (this.dir == i.Vertical) {
//                 var e = this.content.y;
//                 e < 0 ? (e = 0) : e > this.content_height - this.height && (e = this.content_height - this.height);
//                 var t = (r = -e) - this.height;
//                 var n = this.genju_suoyin_jisuan_suoyin(r);
//                 var o = this.genju_suoyin_jisuan_suoyin(t);
//                 n = Math.max(n - this.col, 0);
//                 o = Math.min(this.packItems.length - 1, o + this.col);
//                 if (n != this._qishi_suoyin) {
//                     this._qishi_suoyin = n;
//                     this.renderDirty = !0;
//                 }
//                 if (o != this._tingzhi_suoyin) {
//                     this._tingzhi_suoyin = o;
//                     this.renderDirty = !0;
//                 }
//             } else {
//                 var r;
//                 var a = this.content.x;
//                 a > 0 ? (a = 0) : a < this.width - this.content_width && (a = this.width - this.content_width);
//                 t = (r = -a) + this.width;
//                 n = this.genju_suoyin_jisuan_suoyin(r);
//                 o = this.genju_suoyin_jisuan_suoyin(t);
//                 n = Math.max(n - this.row, 0);
//                 o = Math.min(this.packItems.length - 1, o + this.row);
//                 if (n != this._qishi_suoyin) {
//                     this._qishi_suoyin = n;
//                     this.renderDirty = !0;
//                 }
//                 if (o != this._tingzhi_suoyin) {
//                     this._tingzhi_suoyin = o;
//                     this.renderDirty = !0;
//                 }
//             }
//         }
//     };
//     e.prototype.onUpdate = function () {
//         if (this.scrollview && this.renderDirty && cc.isValid(this.scrollview.node)) {
//             this.render_items();
//             this.renderDirty = !1;
//         }
//     };
//     e.prototype.genju_suoyin_jisuan_suoyin = function (e) {
//         var t;
//         var n = 0;
//         var o = 0;
//         o = t = this.packItems.length - 1;
//         if (this.dir == i.Vertical) {
//             for (; o >= n; ) {
//                 var r = n + Math.floor((o - n) / 2);
//                 var a = this.packItems[r].y;
//                 var s = r < t ? this.packItems[r + 1].y : -this.content_height;
//                 if (e <= a && e >= s) {
//                     return r;
//                 }
//                 e > a ? (o = r - 1) : (n = r + 1);
//             }
//         } else {
//             for (; o >= n; ) {
//                 r = n + Math.floor((o - n) / 2);
//                 a = this.packItems[r].x;
//                 s = r < t ? this.packItems[r + 1].x : this.content_width;
//                 if (e >= a && e <= s) {
//                     return r;
//                 }
//                 e > a ? (n = r + 1) : (o = r - 1);
//             }
//         }
//         return -1;
//     };
//     e.prototype.xuanzhong_shuju = function (e) {
//         var t = this.packItems.findIndex(function (t) {
//             return t.data == e;
//         });
//         if (-1 != t) {
//             this.xuanzhong_item(t);
//         }
//     };
//     e.prototype.xuanzhong_item = function (e) {
//         if (e != this._xuanzhong_de_suoyin) {
//             if (-1 != this._xuanzhong_de_suoyin) {
//                 this.inner_xuanzhong_item(this._xuanzhong_de_suoyin, !1);
//             }
//             this._xuanzhong_de_suoyin = e;
//             this.inner_xuanzhong_item(e, !0);
//         }
//     };
//     e.prototype.inner_xuanzhong_item = function (e, t) {
//         var n = this.packItems[e];
//         n
//             ? ((n.is_select = t), n.item && (n.item.onSetSelect(t, e), t && n.item.onSelected(n.data, e)))
//             : cc.warn("inner_xuanzhong_item index is out of range{", 0, this.packItems.length - 1, "}", e);
//     };
//     e.prototype.spawn_item = function () {
//         var e = this.item_pool.pop();
//         if (!e) {
//             (e = cc.instantiate(this.item_tpl).getComponent(AL_GAME_HUA_listviewitem.ListViewItem)).node.active = !0;
//             AL_GAME_HUA_layout_utils.LayoutUtil.shezhi_pivot_smart(e.node, this.item_anchorX, this.item_anchorY);
//             e.zhixing_chushihua_chuli();
//         }
//         e.node.parent = this.content;
//         return e;
//     };
//     e.prototype.recycle_item = function (e) {
//         var t = e.item;
//         if (t && cc.isValid(t.node)) {
//             t.onRecycle(e.data);
//             t.node.removeFromParent();
//             this.item_pool.push(t);
//             e.item = null;
//         }
//     };
//     e.prototype.qingkong_item_list = function () {
//         var e = this;
//         if (this.packItems) {
//             this.packItems.forEach(function (t) {
//                 e.recycle_item(t);
//             });
//         }
//     };
//     e.prototype.render_items = function () {
//         var e;
//         for (var t = 0; t < this._qishi_suoyin; t++) {
//             if ((e = this.packItems[t]).item) {
//                 this.recycle_item(e);
//             }
//         }
//         for (t = this.packItems.length - 1; t > this._tingzhi_suoyin; t--) {
//             if ((e = this.packItems[t]).item) {
//                 this.recycle_item(e);
//             }
//         }
//         for (t = this._qishi_suoyin; t <= this._tingzhi_suoyin; t++) {
//             (e = this.packItems[t]).item ||
//                 ((e.item = this.spawn_item(t)),
//                 e.item.onSetData(e.data, t),
//                 e.item.onSetSelect(e.is_select, t),
//                 e.is_select && e.item.onSelected(e.data, t));
//             e.item.setLeftTop(e.x, e.y);
//         }
//     };
//     e.prototype.pack_item = function (e) {
//         return {
//             x: 0,
//             y: 0,
//             data: e,
//             item: null,
//             is_select: !1
//         };
//     };
//     e.prototype.layout_items = function (e) {
//         var t;
//         var n;
//         var o = e;
//         for (var r = this.packItems.length; o < r; o++) {
//             var s = this.packItems[o];
//             this.dir == i.Vertical
//                 ? ((t = AL_GAME_HUA_layout_utils.LayoutUtil.jisuan_chuizhi_buju(
//                       o,
//                       this.item_width,
//                       this.item_height,
//                       this.col,
//                       this.gap_x,
//                       this.gap_y,
//                       this.padding_left,
//                       this.padding_top
//                   )),
//                   (s.x = t[0]),
//                   (s.y = t[1]))
//                 : ((n = AL_GAME_HUA_layout_utils.LayoutUtil.jisuan_shuiping_buju(
//                       o,
//                       this.item_width,
//                       this.item_height,
//                       this.row,
//                       this.gap_x,
//                       this.gap_y,
//                       this.padding_left,
//                       this.padding_top
//                   )),
//                   (s.x = n[0]),
//                   (s.y = n[1]));
//         }
//     };
//     e.prototype.adjust_content = function () {
//         if (this.packItems.length <= 0) {
//             this.set_content_size(0, 0);
//         } else {
//             {
//                 var e = this.packItems[this.packItems.length - 1];
//                 if (this.dir == i.Vertical) {
//                     var t = Math.max(this.height, this.item_height - e.y + this.padding_bottom);
//                     this.set_content_size(this.content_width, t);
//                 } else {
//                     var n = Math.max(this.width, e.x + this.item_width + this.padding_right);
//                     this.set_content_size(n, this.content_height);
//                 }
//             }
//         }
//     };
//     e.prototype.set_content_size = function (e, t) {
//         if (this.content_width != e) {
//             this.content_width = e;
//             this.content.width = e;
//         }
//         if (this.content_height != t) {
//             this.content_height = t;
//             this.content.height = t;
//         }
//     };
//     e.prototype.set_viewport = function (e, t) {
//         null == e ? (e = this.width) : e > this.content_width && (e = this.content_width);
//         null == t ? (t = this.height) : t > this.content_height && (t = this.content_height);
//         this.width = e;
//         this.height = t;
//         this.mask.node.setContentSize(e, t);
//         this.scrollview.node.setContentSize(e, t);
//         this.render();
//     };
//     e.prototype.renderAll = function (e) {
//         var t;
//         var n;
//         e
//             ? ((t = this.content_width), (n = this.content_height))
//             : ((t = this.original_width), (n = this.original_height));
//         this.set_viewport(t, n);
//     };
//     e.prototype.set_data = function (e) {
//         var t = this;
//         this.packItems ? (this.qingkong_item_list(), (this.packItems.length = 0)) : (this.packItems = []);
//         e.forEach(function (e) {
//             var n = t.pack_item(e);
//             t.packItems.push(n);
//         });
//         this.layout_items(0);
//         this.adjust_content();
//         this._qishi_suoyin = -1;
//         this._tingzhi_suoyin = -1;
//         this.dir == i.Vertical ? (this.content.y = 0) : (this.content.x = 0);
//         if (this.packItems.length > 0) {
//             this.render();
//         }
//     };
//     e.prototype.churu_shuju = function (e) {
//         var t;
//         var n = this;
//         var i = [];
//         for (var r = 1; r < arguments.length; r++) {
//             i[r - 1] = arguments[r];
//         }
//         if (0 != i.length) {
//             if (!this.packItems) {
//                 this.packItems = [];
//             }
//             if (e < 0 || e > this.packItems.length) {
//                 cc.warn("churu_shuju, invalid index", e);
//             } else {
//                 {
//                     var a = e == this.packItems.length;
//                     var s = [];
//                     i.forEach(function (e) {
//                         var t = n.pack_item(e);
//                         s.push(t);
//                     });
//                     (t = this.packItems).splice.apply(t, __spreadArrays([e, 0], s));
//                     this.layout_items(e);
//                     this.adjust_content();
//                     this._qishi_suoyin = -1;
//                     this._tingzhi_suoyin = -1;
//                     if (this.auto_scrolling && a) {
//                         this.huadong_dao_zuihou();
//                     }
//                     this.render();
//                 }
//             }
//         } else {
//             cc.log("nothing to insert");
//         }
//     };
//     e.prototype.yichu_shuju = function (e, t) {
//         var n = this;
//         if (void 0 === t) {
//             t = 1;
//         }
//         if (this.packItems) {
//             if (e < 0 || e >= this.packItems.length) {
//                 cc.warn("yichu_shuju, invalid index", e);
//             } else {
//                 if (t < 1) {
//                     cc.log("nothing to remove");
//                 } else {
//                     {
//                         var o = this.packItems.length;
//                         this.packItems.splice(e, t).forEach(function (e) {
//                             n.recycle_item(e);
//                         });
//                         if (e + t < o) {
//                             this.layout_items(e);
//                         }
//                         this.adjust_content();
//                         if (this.packItems.length > 0) {
//                             this._qishi_suoyin = -1;
//                             this._tingzhi_suoyin = -1;
//                             this.render();
//                         }
//                     }
//                 }
//             }
//         } else {
//             cc.log("call set_data before call this method");
//         }
//     };
//     e.prototype.append_data = function () {
//         var e = [];
//         for (var t = 0; t < arguments.length; t++) {
//             e[t] = arguments[t];
//         }
//         if (!this.packItems) {
//             this.packItems = [];
//         }
//         this.churu_shuju.apply(this, __spreadArrays([this.packItems.length], e));
//     };
//     e.prototype.huadong_dao = function (e, t) {
//         if (void 0 === t) {
//             t = 0;
//         }
//         if (this.packItems) {
//             var n = this.packItems[e];
//             if (n) {
//                 if (this.dir == i.Vertical) {
//                     var o = this.height - this.content_height;
//                     if (o >= 0) {
//                         return void cc.log("no need to scroll");
//                     }
//                     if ((s = n.y) < o) {
//                         s = o;
//                         cc.log("content reach bottom");
//                     }
//                     var r = this.content.x;
//                     0 == t
//                         ? this.scrollview.setContentPosition(cc.v2(r, -s))
//                         : this.scrollview.scrollToOffset(cc.v2(r, -s), t);
//                     this.render();
//                 } else {
//                     var a = this.content_width - this.width;
//                     if (a <= 0) {
//                         return void cc.log("no need to scroll");
//                     }
//                     if ((r = n.x) > a) {
//                         r = a;
//                         cc.log("content reach right");
//                     }
//                     var s = this.content.y;
//                     0 == t
//                         ? this.scrollview.setContentPosition(cc.v2(-r, s))
//                         : this.scrollview.scrollToOffset(cc.v2(-r, s), t);
//                     this.render();
//                 }
//             } else {
//                 cc.log("huadong_dao, index out of range");
//             }
//         }
//     };
//     e.prototype.huoqu_huadong_pianyi = function () {
//         var e = this.scrollview.getScrollOffset();
//         return this.dir == i.Vertical ? e.y : e.x;
//     };
//     e.prototype.huadong_dao_pianyi = function (e, t) {
//         if (void 0 === t) {
//             t = 0;
//         }
//         if (this.dir == i.Vertical) {
//             var n = this.content.x;
//             0 == t ? this.scrollview.setContentPosition(cc.v2(n, e)) : this.scrollview.scrollToOffset(cc.v2(n, e), t);
//             this.render();
//         } else {
//             var o = this.content.y;
//             0 == t ? this.scrollview.setContentPosition(cc.v2(e, o)) : this.scrollview.scrollToOffset(cc.v2(e, o), t);
//             this.render();
//         }
//     };
//     e.prototype.huadong_dao_zuihou = function () {
//         this.dir == i.Vertical ? this.scrollview.scrollToBottom() : this.scrollview.scrollToRight();
//     };
//     e.prototype.shuaxin_item = function (e, t) {
//         var n = this.huoqu_pack_item(e);
//         if (n) {
//             var o = n.data;
//             n.data = t;
//             if (n.item) {
//                 n.item.onRecycle(o);
//                 n.item.onSetData(t, e);
//             }
//         }
//     };
//     e.prototype.chongxin_jiazai_item = function (e) {
//         var t = this.huoqu_pack_item(e);
//         if (t && t.item) {
//             t.item.onRecycle(t.data);
//             t.item.onSetData(t.data, e);
//         }
//     };
//     e.prototype.huoqu_pack_item = function (e) {
//         return this.packItems
//             ? e < 0 || e >= this.packItems.length
//                 ? (cc.warn("huoqu_pack_item, invalid index", e), null)
//                 : this.packItems[e]
//             : (cc.log("call set_data before call this method"), null);
//     };
//     e.prototype.huoqu_tem = function (e) {
//         var t = this.huoqu_pack_item(e);
//         return t ? t.item : null;
//     };
//     e.prototype.huoqu_shuju = function (e) {
//         var t = this.huoqu_pack_item(e);
//         return t ? t.data : null;
//     };
//     e.prototype.chazhao_item = function (e) {
//         if (!this.packItems || !this.packItems.length) {
//             cc.log("call set_data before call this method");
//             return null;
//         }
//         for (var t = this._qishi_suoyin; t <= this._tingzhi_suoyin; t++) {
//             var n = this.packItems[t];
//             if (e(n.data)) {
//                 return n.item;
//             }
//         }
//         return null;
//     };
//     e.prototype.chazhao_suoyin = function (e) {
//         return this.packItems && this.packItems.length
//             ? this.packItems.findIndex(function (t) {
//                   return e(t.data);
//               })
//             : (cc.log("call set_data before call this method"), -1);
//     };
//     Object.defineProperty(e.prototype, "renderedItems", {
//         get: function () {
//             var e = [];
//             for (var t = this._qishi_suoyin; t <= this._tingzhi_suoyin; t++) {
//                 var n = this.packItems[t];
//                 if (n && n.item) {
//                     e.push(n.item);
//                 }
//             }
//             return e;
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     Object.defineProperty(e.prototype, "length", {
//         get: function () {
//             return this.packItems ? this.packItems.length : (cc.log("call set_data before call this method"), 0);
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     e.prototype.destroy = function () {
//         this.qingkong_item_list();
//         this.item_pool.forEach(function (e) {
//             e.onUnInit();
//             e.node.destroy();
//         });
//         this.item_pool = null;
//         this.packItems = null;
//         this.renderDirty = null;
//         if (cc.isValid(this.scrollview.node)) {
//             this.scrollview.node.off("scrolling", this.on_scrolling, this);
//             this.scrollview.node.off("scroll-to-bottom", this.on_scroll_to_end, this);
//             this.scrollview.node.off("scroll-to-right", this.on_scroll_to_end, this);
//             this.scrollview.node.off(cc.Node.EventType.TOUCH_START, this.on_scroll_touch_start, this);
//             this.scrollview.node.off(cc.Node.EventType.TOUCH_END, this.on_scroll_touch_end, this);
//             this.scrollview.node.off(cc.Node.EventType.TOUCH_CANCEL, this.on_scroll_touch_cancel, this);
//         }
//     };
//     Object.defineProperty(e.prototype, "xuanzhong_de_suoyin", {
//         get: function () {
//             return this._xuanzhong_de_suoyin;
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     Object.defineProperty(e.prototype, "xuanzhong_de_shuju", {
//         get: function () {
//             var e = this.packItems[this._xuanzhong_de_suoyin];
//             return e ? e.data : null;
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     Object.defineProperty(e.prototype, "shfou_keyi_huadong", {
//         set: function (e) {
//             this.dir == i.Vertical ? (this.scrollview.vertical = e) : (this.scrollview.horizontal = e);
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     Object.defineProperty(e.prototype, "qishi_suoyin", {
//         get: function () {
//             return this._qishi_suoyin;
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     Object.defineProperty(e.prototype, "tingzhi_suoyin", {
//         get: function () {
//             return this._tingzhi_suoyin;
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     return e;
// })();
// exports.ListView = c;
// (r = i = exports.ListViewDir || (exports.ListViewDir = {}))[(r.Vertical = 1)] = "Vertical";
// r[(r.Horizontal = 2)] = "Horizontal";



exports.ListViewDir = exports.ListView = void 0;

// 导入模块
const LayoutUtils = require("./AL_GAME_HUA_layout_utils");
const ListViewItem = require("./AL_GAME_HUA_listviewitem");

/**
 * Represents a ListView component.
 */
class ListView {
    constructor(config) {
        this._xuanzhong_de_suoyin = -1;
        const { scrollview, mask, content, item_tpl, direction, width, height, gap_x, gap_y, 
                padding_left, padding_right, padding_top, padding_bottom, item_anchorX, item_anchorY, 
                row, column, cb_host, scroll_to_end_cb, auto_scrolling } = config;

        this.scrollview = scrollview;
        this.mask = mask;
        this.content = content;
        this.item_tpl = item_tpl;
        this.item_width = item_tpl instanceof cc.Node ? item_tpl.width : item_tpl.data.width;
        this.item_height = item_tpl instanceof cc.Node ? item_tpl.height : item_tpl.data.height;

        if (item_tpl instanceof cc.Node) {
            item_tpl.active = false;
        }

        this.dir = direction || ListViewDir.Vertical;
        this.width = width || scrollview.node.width;
        this.height = height || scrollview.node.height;
        this.gap_x = gap_x || 0;
        this.gap_y = gap_y || 0;
        this.padding_left = padding_left || 0;
        this.padding_right = padding_right || 0;
        this.padding_top = padding_top || 0;
        this.padding_bottom = padding_bottom || 0;
        this.item_anchorX = item_anchorX != null ? item_anchorX : 0;
        this.item_anchorY = item_anchorY != null ? item_anchorY : 1;
        this.row = row || 1;
        this.col = column || 1;
        this.cb_host = cb_host;
        this.scroll_to_end_cb = scroll_to_end_cb;
        this.auto_scrolling = auto_scrolling || false;
        this.item_pool = [];

        const contentWidth = (this.item_width + this.gap_x) * this.col - this.gap_x + this.padding_left + this.padding_right;
        const contentHeight = (this.item_height + this.gap_y) * this.row - this.gap_y + this.padding_top + this.padding_bottom;

        if (this.dir === ListViewDir.Vertical) {
            if (contentWidth > this.width) {
                cc.log("content_width > width, resize listview to content_width,", this.width, "->", contentWidth);
                this.width = contentWidth;
            }
            this.setContentSize(this.width, 0);
        } else {
            if (contentHeight > this.height) {
                cc.log("content_height > height, resize listview to content_height,", this.height, "->", contentHeight);
                this.height = contentHeight;
            }
            this.setContentSize(0, this.height);
        }
        
        this.original_width = this.width;
        this.original_height = this.height;
        this.mask.node.setContentSize(this.width, this.height);
        this.scrollview.node.setContentSize(this.width, this.height);
        this.scrollview.vertical = this.dir === ListViewDir.Vertical;
        this.scrollview.horizontal = this.dir === ListViewDir.Horizontal;
        this.scrollview.inertia = true;
        this.scrollview.node.on("scrolling", this.onScrolling, this);
        this.scrollview.node.on("scroll-to-bottom", this.onScrollToEnd, this);
        this.scrollview.node.on("scroll-to-right", this.onScrollToEnd, this);
        this.scrollview.node.on(cc.Node.EventType.TOUCH_START, this.onScrollTouchStart, this);
        this.scrollview.node.on(cc.Node.EventType.TOUCH_END, this.onScrollTouchEnd, this);
        this.scrollview.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onScrollTouchCancel, this);
    }

    onScrollTouchStart(event) {
        this._touchBeganPosition = event.touch.getLocation();
    }

    onScrollTouchCancel(event) {
        this._touchEndPosition = event.touch.getLocation();
        this.handleReleaseLogic();
    }

    onScrollTouchEnd(event) {
        this._touchEndPosition = event.touch.getLocation();
        this.handleReleaseLogic();
    }

    handleReleaseLogic() {
        if (this._touchBeganPosition) {
            const endPosition = this._touchEndPosition;
            const dragVector = this._touchBeganPosition.sub(this._touchEndPosition);
            if (this.getDragDirection(dragVector) === 0 && this.packItems && this.packItems.length) {
                const localPosition = this.content.convertToNodeSpaceAR(endPosition);
                for (let i = this._qishi_suoyin; i <= this._tingzhi_suoyin; i++) {
                    const packItem = this.packItems[i];
                    if (packItem && packItem.item && packItem.item.node.getBoundingBox().contains(localPosition)) {
                        packItem.item.handle_event_touch_end(packItem.item.node.convertToNodeSpaceAR(endPosition), packItem.data, i);
                        break;
                    }
                }
            }
        }
    }

    getDragDirection(vector) {
        return this.dir === ListViewDir.Horizontal ? Math.abs(vector.x) < 3 ? 0 : vector.x > 0 ? 1 : -1 :
            this.dir === ListViewDir.Vertical ? Math.abs(vector.y) < 3 ? 0 : vector.y < 0 ? 1 : -1 : void 0;
    }

    onScrollToEnd() {
        if (this.scroll_to_end_cb) {
            this.scroll_to_end_cb.call(this.cb_host);
        }
    }

    onScrolling() {
        let currentPosition = (this.dir === ListViewDir.Vertical) ? this.content.y : this.content.x;
        const dimension = (this.dir === ListViewDir.Vertical) ? this.item_height : this.item_width;

        if (!(this.last_content_pos != null && Math.abs(currentPosition - this.last_content_pos) < dimension)) {
            this.last_content_pos = currentPosition;
            this.render();
        }
    }

    render() {
        if (this.packItems && this.packItems.length) {
            if (this.dir === ListViewDir.Vertical) {
                let currentContentPosition = this.content.y;
                currentContentPosition = Math.max(0, Math.min(currentContentPosition, this.content_height - this.height));

                const upperBound = -currentContentPosition;
                const lowerBound = upperBound - this.height;
                let qishi_suoyin = this.genju_suoyin_jisuan_suoyin(upperBound);
                let tingzhi_suoyin = this.genju_suoyin_jisuan_suoyin(lowerBound);

                qishi_suoyin = Math.max(qishi_suoyin - this.col, 0);
                tingzhi_suoyin = Math.min(this.packItems.length - 1, tingzhi_suoyin + this.col);

                if (qishi_suoyin !== this._qishi_suoyin) {
                    this._qishi_suoyin = qishi_suoyin;
                    this.renderDirty = true;
                }
                if (tingzhi_suoyin !== this._tingzhi_suoyin) {
                    this._tingzhi_suoyin = tingzhi_suoyin;
                    this.renderDirty = true;
                }
            } else {
                let currentContentPosition = this.content.x;
                currentContentPosition = Math.max(this.width - this.content_width, Math.min(0, currentContentPosition));

                const rightBorder = -currentContentPosition + this.width;
                let qishi_suoyin = this.genju_suoyin_jisuan_suoyin(-currentContentPosition);
                let tingzhi_suoyin = this.genju_suoyin_jisuan_suoyin(rightBorder);

                qishi_suoyin = Math.max(qishi_suoyin - this.row, 0);
                tingzhi_suoyin = Math.min(this.packItems.length - 1, tingzhi_suoyin + this.row);

                if (qishi_suoyin !== this._qishi_suoyin) {
                    this._qishi_suoyin = qishi_suoyin;
                    this.renderDirty = true;
                }
                if (tingzhi_suoyin !== this._tingzhi_suoyin) {
                    this._tingzhi_suoyin = tingzhi_suoyin;
                    this.renderDirty = true;
                }
            }
        }
    }

    onUpdate() {
        if (this.scrollview && this.renderDirty && cc.isValid(this.scrollview.node)) {
            this.renderItems();
            this.renderDirty = false;
        }
    }

    genju_suoyin_jisuan_suoyin(offset) {
        let start = 0;
        let end = this.packItems.length - 1;
        let indexMax = end;

        if (this.dir === ListViewDir.Vertical) {
            while (end >= start) {
                const mid = Math.floor((end + start) / 2);
                const midY = this.packItems[mid].y;
                const nextY = (mid < indexMax) ? this.packItems[mid + 1].y : -this.content_height;

                if (offset <= midY && offset >= nextY) {
                    return mid;
                }

                if (offset > midY) {
                    end = mid - 1;
                } else {
                    start = mid + 1;
                }
            }
        } else {
            while (end >= start) {
                const mid = Math.floor((end + start) / 2);
                const midX = this.packItems[mid].x;
                const nextX = (mid < indexMax) ? this.packItems[mid + 1].x : this.content_width;

                if (offset >= midX && offset <= nextX) {
                    return mid;
                }

                if (offset < midX) {
                    end = mid - 1;
                } else {
                    start = mid + 1;
                }
            }
        }
        return -1;
    }

    xuanzhong_shuju(data) {
        const index = this.packItems.findIndex(item => item.data === data);
        if (index !== -1) {
            this.xuanzhong_item(index);
        }
    }

    xuanzhong_item(index) {
        if (index !== this._xuanzhong_de_suoyin) {
            if (this._xuanzhong_de_suoyin !== -1) {
                this.inner_xuanzhong_item(this._xuanzhong_de_suoyin, false);
            }
            this._xuanzhong_de_suoyin = index;
            this.inner_xuanzhong_item(index, true);
        }
    }

    inner_xuanzhong_item(index, isSelected) {
        const packItem = this.packItems[index];
        if (packItem) {
            packItem.is_select = isSelected;
            if (packItem.item) {
                packItem.item.onSetSelect(isSelected, index);
                if (isSelected) {
                    packItem.item.onSelected(packItem.data, index);
                }
            }
        } else {
            cc.warn("inner_xuanzhong_item index is out of range{", 0, this.packItems.length - 1, "}", index);
        }
    }

    spawn_item(index) {
        let item = this.item_pool.pop();
        if (!item) {
            item = cc.instantiate(this.item_tpl).getComponent(ListViewItem.ListViewItem);
            item.node.active = true;
            LayoutUtils.LayoutUtil.shezhi_pivot_smart(item.node, this.item_anchorX, this.item_anchorY);
            item.zhixing_chushihua_chuli();
        }
        item.node.parent = this.content;
        return item;
    }

    recycle_item(packItem) {
        const { item } = packItem;
        if (item && cc.isValid(item.node)) {
            item.onRecycle(packItem.data);
            item.node.removeFromParent();
            this.item_pool.push(item);
            packItem.item = null;
        }
    }

    qingkong_item_list() {
        if (this.packItems) {
            this.packItems.forEach(packItem => this.recycle_item(packItem));
        }
    }

    renderItems() {
        for (let i = 0; i < this._qishi_suoyin; i++) {
            const packItem = this.packItems[i];
            if (packItem.item) {
                this.recycle_item(packItem);
            }
        }
        
        for (let i = this.packItems.length - 1; i > this._tingzhi_suoyin; i--) {
            const packItem = this.packItems[i];
            if (packItem.item) {
                this.recycle_item(packItem);
            }
        }
        
        for (let i = this._qishi_suoyin; i <= this._tingzhi_suoyin; i++) {
            let packItem = this.packItems[i];
            if (!packItem.item) {
                packItem.item = this.spawn_item(i);
                packItem.item.onSetData(packItem.data, i);
                packItem.item.onSetSelect(packItem.is_select, i);
                if (packItem.is_select) {
                    packItem.item.onSelected(packItem.data, i);
                }
            }
            packItem.item.setLeftTop(packItem.x, packItem.y);
        }
    }

    pack_item(dataItem) {
        return {
            x: 0,
            y: 0,
            data: dataItem,
            item: null,
            is_select: false
        };
    }

    layout_items(qishi_suoyin) {
        for (let i = qishi_suoyin; i < this.packItems.length; i++) {
            const packItem = this.packItems[i];
            let position;
            if (this.dir === ListViewDir.Vertical) {
                position = LayoutUtils.LayoutUtil.jisuan_chuizhi_buju(i, this.item_width, this.item_height, this.col, this.gap_x, this.gap_y, this.padding_left, this.padding_top);
            } else {
                position = LayoutUtils.LayoutUtil.jisuan_shuiping_buju(i, this.item_width, this.item_height, this.row, this.gap_x, this.gap_y, this.padding_left, this.padding_top);
            }
            [packItem.x, packItem.y] = position;
        }
    }

    adjust_content() {
        if (this.packItems.length <= 0) {
            this.setContentSize(0, 0);
        } else {
            const lastItem = this.packItems[this.packItems.length - 1];
            if (this.dir === ListViewDir.Vertical) {
                const contentHeight = Math.max(this.height, this.item_height - lastItem.y + this.padding_bottom);
                this.setContentSize(this.content_width, contentHeight);
            } else {
                const contentWidth = Math.max(this.width, lastItem.x + this.item_width + this.padding_right);
                this.setContentSize(contentWidth, this.content_height);
            }
        }
    }

    setContentSize(newWidth, newHeight) {
        if (this.content_width !== newWidth) {
            this.content_width = newWidth;
            this.content.width = newWidth;
        }
        if (this.content_height !== newHeight) {
            this.content_height = newHeight;
            this.content.height = newHeight;
        }
    }

    setViewport(viewportWidth, viewportHeight) {
        viewportWidth == null && (viewportWidth = this.width) || viewportWidth > this.content_width && (viewportWidth = this.content_width);
        viewportHeight == null && (viewportHeight = this.height) || viewportHeight > this.content_height && (viewportHeight = this.content_height);

        this.width = viewportWidth;
        this.height = viewportHeight;
        this.mask.node.setContentSize(viewportWidth, viewportHeight);
        this.scrollview.node.setContentSize(viewportWidth, viewportHeight);
        this.render();
    }

    renderAll(flag) {
        const targetWidth = flag ? this.content_width : this.original_width;
        const targetHeight = flag ? this.content_height : this.original_height;
        this.setViewport(targetWidth, targetHeight);
    }

    set_data(dataArray) {
        this.packItems ? (this.qingkong_item_list(), this.packItems.length = 0) : this.packItems = [];
        dataArray.forEach(dataItem => this.packItems.push(this.pack_item(dataItem)));
        this.layout_items(0);
        this.adjust_content();

        this._qishi_suoyin = -1;
        this._tingzhi_suoyin = -1;
        if (this.dir === ListViewDir.Vertical) {
            this.content.y = 0;
        } else {
            this.content.x = 0;
        }
        if (this.packItems.length > 0) {
            this.render();
        }
    }

    churu_shuju(index, ...newData) {
        if (newData.length === 0) {
            cc.log("nothing to insert");
            return;
        }

        if (!this.packItems) {
            this.packItems = [];
        }

        if (index < 0 || index > this.packItems.length) {
            cc.warn("churu_shuju, invalid index", index);
            return;
        }

        const itemsToInsert = newData.map(dataItem => this.pack_item(dataItem));
        this.packItems.splice(index, 0, ...itemsToInsert);
        this.layout_items(index);
        this.adjust_content();

        this._qishi_suoyin = -1;
        this._tingzhi_suoyin = -1;

        if (this.auto_scrolling && index === this.packItems.length) {
            this.huadong_dao_zuihou();
        }
        this.render();
    }

    yichu_shuju(index, count = 1) {
        if (!this.packItems) {
            cc.log("call set_data before call this method");
            return;
        }

        if (index < 0 || index >= this.packItems.length) {
            cc.warn("yichu_shuju, invalid index", index);
            return;
        }

        if (count < 1) {
            cc.log("nothing to remove");
            return;
        }

        const initialLength = this.packItems.length;
        this.packItems.splice(index, count).forEach(packItem => this.recycle_item(packItem));

        if (index + count < initialLength) {
            this.layout_items(index);
        }
        this.adjust_content();

        if (this.packItems.length > 0) {
            this._qishi_suoyin = -1;
            this._tingzhi_suoyin = -1;
            this.render();
        }
    }

    append_data(...newData) {
        if (!this.packItems) {
            this.packItems = [];
        }
        this.churu_shuju(this.packItems.length, ...newData);
    }

    huadong_dao(index, duration = 0) {
        if (!this.packItems) {
            return;
        }

        const targetPackItem = this.packItems[index];

        if (!targetPackItem) {
            cc.log("huadong_dao, index out of range");
            return;
        }

        if (this.dir === ListViewDir.Vertical) {
            const bottomLimit = this.height - this.content_height;
            if (bottomLimit >= 0) {
                cc.log("no need to scroll");
                return;
            }

            let targetYPosition = targetPackItem.y;
            targetYPosition = Math.max(bottomLimit, targetYPosition);

            const currentXPosition = this.content.x;
            if (duration === 0) {
                this.scrollview.setContentPosition(cc.v2(currentXPosition, -targetYPosition));
            } else {
                this.scrollview.scrollToOffset(cc.v2(currentXPosition, -targetYPosition), duration);
            }
        } else {
            const rightLimit = this.content_width - this.width;
            if (rightLimit <= 0) {
                cc.log("no need to scroll");
                return;
            }

            let targetXPosition = targetPackItem.x;
            targetXPosition = Math.min(rightLimit, targetXPosition);

            const currentYPosition = this.content.y;
            if (duration === 0) {
                this.scrollview.setContentPosition(cc.v2(-targetXPosition, currentYPosition));
            } else {
                this.scrollview.scrollToOffset(cc.v2(-targetXPosition, currentYPosition), duration);
            }
        }
        this.render();
    }

    huoqu_huadong_pianyi() {
        const scrollOffset = this.scrollview.getScrollOffset();
        return this.dir === ListViewDir.Vertical ? scrollOffset.y : scrollOffset.x;
    }

    huadong_dao_pianyi(offset, duration = 0) {
        if (this.dir === ListViewDir.Vertical) {
            const currentXPosition = this.content.x;
            if (duration === 0) {
                this.scrollview.setContentPosition(cc.v2(currentXPosition, offset));
            } else {
                this.scrollview.scrollToOffset(cc.v2(currentXPosition, offset), duration);
            }
        } else {
            const currentYPosition = this.content.y;
            if (duration === 0) {
                this.scrollview.setContentPosition(cc.v2(offset, currentYPosition));
            } else {
                this.scrollview.scrollToOffset(cc.v2(offset, currentYPosition), duration);
            }
        }
        this.render();
    }

    huadong_dao_zuihou() {
        if (this.dir === ListViewDir.Vertical) {
            this.scrollview.scrollToBottom();
        } else {
            this.scrollview.scrollToRight();
        }
    }

    shuaxin_item(index, newData) {
        const packItem = this.huoqu_pack_item(index);
        if (packItem) {
            const oldData = packItem.data;
            packItem.data = newData;
            if (packItem.item) {
                packItem.item.onRecycle(oldData);
                packItem.item.onSetData(newData, index);
            }
        }
    }

    chongxin_jiazai_item(index) {
        const packItem = this.huoqu_pack_item(index);
        if (packItem && packItem.item) {
            packItem.item.onRecycle(packItem.data);
            packItem.item.onSetData(packItem.data, index);
        }
    }

    huoqu_pack_item(index) {
        if (!this.packItems) {
            cc.log("call set_data before call this method");
            return null;
        }

        if (index < 0 || index >= this.packItems.length) {
            cc.warn("huoqu_pack_item, invalid index", index);
            return null;
        }

        return this.packItems[index];
    }

    huoqu_tem(index) {
        const packItem = this.huoqu_pack_item(index);
        return packItem ? packItem.item : null;
    }

    huoqu_shuju(index) {
        const packItem = this.huoqu_pack_item(index);
        return packItem ? packItem.data : null;
    }

    chazhao_item(predicate) {
        if (!this.packItems || this.packItems.length === 0) {
            cc.log("call set_data before call this method");
            return null;
        }

        for (let i = this._qishi_suoyin; i <= this._tingzhi_suoyin; i++) {
            const packItem = this.packItems[i];
            if (predicate(packItem.data)) {
                return packItem.item;
            }
        }
        return null;
    }

    chazhao_suoyin(predicate) {
        return this.packItems && this.packItems.length
            ? this.packItems.findIndex(packItem => predicate(packItem.data))
            : (cc.log("call set_data before call this method"), -1);
    }

    destroy() {
        this.qingkong_item_list();
        this.item_pool.forEach(item => {
            item.onUnInit();
            item.node.destroy();
        });
        this.item_pool = null;
        this.packItems = null;
        this.renderDirty = null;

        if (cc.isValid(this.scrollview.node)) {
            this.scrollview.node.off("scrolling", this.onScrolling, this);
            this.scrollview.node.off("scroll-to-bottom", this.onScrollToEnd, this);
            this.scrollview.node.off("scroll-to-right", this.onScrollToEnd, this);
            this.scrollview.node.off(cc.Node.EventType.TOUCH_START, this.onScrollTouchStart, this);
            this.scrollview.node.off(cc.Node.EventType.TOUCH_END, this.onScrollTouchEnd, this);
            this.scrollview.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onScrollTouchCancel, this);
        }
    }

    get renderedItems() {
        return this.packItems
            .slice(this._qishi_suoyin, this._tingzhi_suoyin + 1)
            .map(packItem => packItem.item)
            .filter(item => item != null);
    }

    get length() {
        if (!this.packItems) {
            cc.log("call set_data before call this method");
            return 0;
        }
        return this.packItems.length;
    }

    get xuanzhong_de_suoyin() {
        return this._xuanzhong_de_suoyin;
    }

    get xuanzhong_de_shuju() {
        const selectedItem = this.packItems[this._xuanzhong_de_suoyin];
        return selectedItem ? selectedItem.data : null;
    }

    set shfou_keyi_huadong(isScrollable) {
        if (this.dir === ListViewDir.Vertical) {
            this.scrollview.vertical = isScrollable;
        } else {
            this.scrollview.horizontal = isScrollable;
        }
    }

    get qishi_suoyin() {
        return this._qishi_suoyin;
    }

    get tingzhi_suoyin() {
        return this._tingzhi_suoyin;
    }
}

exports.ListView = ListView;

// 方向枚举
var i, r;
(r = i = exports.ListViewDir || (exports.ListViewDir = {}))[(r.Vertical = 1)] = "Vertical";
r[(r.Horizontal = 2)] = "Horizontal";
